/*
 * Created: Author: Aléx Carvalho / Date: Apr 9, 2015
 * 
 * Changed: $Author: ac280593@gmail.com $ / $Date: 2015-06-11 04:28:43 +0000 (Thu, 11 Jun 2015) $
 */
package test;

import com.cpm.ServiceLocator;
import com.cpm.entities.Folder;
import com.cpm.entities.Indicator;
import com.cpm.entities.StrategicPlan;
import com.cpm.entities.User;
import com.cpm.entities.id.IntegerId;
import com.cpm.entities.types.Family;
import com.cpm.entities.types.IndicatorDirectionType;
import com.cpm.entities.types.IndicatorModeType;
import com.cpm.entities.types.IndicatorPeriodicity;
import com.cpm.wrapper.Limit;
import com.cpm.wrapper.Password;
import com.cpm.wrapper.Performance;
import com.cpm.wrapper.Position;
import com.cpm.wrapper.Weight;
import java.io.IOException;
import java.time.LocalDate;
import java.time.Month;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/insert")
public class InitialInsert extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        if (ServiceLocator.getUserRepository().findById(new IntegerId(1)) == null) {

            LocalDate start = LocalDate.of(2015, Month.JANUARY, 1);
            LocalDate end = LocalDate.of(2015, Month.DECEMBER, 31);

            User user = new User("admin", "admin", null, new Password("admin"), null, null);
            ServiceLocator.getUserRepository().persist(user);

            StrategicPlan strategicPlan = new StrategicPlan(user, new Limit(60), new Limit(80), new Limit(100), new Performance(1f), "Plano Estrategico", start, end);
            ServiceLocator.getStrategicPlanRepository().persist(strategicPlan);

            for (int i = 0; i < 4; i++) {
                Folder folder = new Folder(strategicPlan.getId(), Family.STRATEGIC_PLAN, null, "Folder " + i, null, user, strategicPlan, new Position((short)i));

                ServiceLocator.getFolderRepository().persist(folder);
            }

            Folder folder = ServiceLocator.getFolderRepository().findById(new IntegerId(1));

            for (int i = 1; i < 10; i++) {

                Indicator indicator = new Indicator(folder.getId(), Family.FOLDER, new Weight((short)1), "R$",
                                                    IndicatorDirectionType.DIRECT, IndicatorPeriodicity.MONTHTY, 
                                                    IndicatorModeType.INDIVIDUAL,
                                                    user, strategicPlan, "Indicador " + i, null,
                                                    start, end, new Position((short)i), null);

                ServiceLocator.getIndicatorRepository().persist(indicator);
            }
            
            resp.getWriter().print("Insert inicial realizado com sucesso!");
        }
        else
        {
            resp.getWriter().print("O Insert inicial já foi realizado!");
        }
    }
}
